package com.samapp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.samapp.entity.User;
import com.samapp.indent.DetectionItemActivity;
import com.samapp.indent.IndentManageActivity;
import com.samapp.map.ClientDistributionActivity;
import com.samapp.settings.SettingsManageActivity;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView clientManageImageView = null;
	private ImageView indentManageImageView = null;
	private ImageView settingsImageView = null;
	private ImageView detectionItemImageView = null;
	private ImageView foodAdditivesImageView = null;
	private ImageView exitLoginImageView = null;
	//public static final int CODE_DETECTIONITEM_MAIN = 0x500;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initListener();
	}

	private void initView() {
		clientManageImageView = (ImageView) findViewById(R.id.imageview_client);
		indentManageImageView = (ImageView) findViewById(R.id.imageview_indent);
		settingsImageView = (ImageView) findViewById(R.id.imageview_settings);
		detectionItemImageView = (ImageView) findViewById(R.id.imageview_detection_item);
		foodAdditivesImageView = (ImageView) findViewById(R.id.imageview_food_additives);
		exitLoginImageView = (ImageView) findViewById(R.id.imageview_exit);
	}

	private void initListener() {
		clientManageImageView.setOnClickListener(this);
		indentManageImageView.setOnClickListener(this);
		settingsImageView.setOnClickListener(this);
		detectionItemImageView.setOnClickListener(this);
		foodAdditivesImageView.setOnClickListener(this);
		exitLoginImageView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageview_client:
			skip(ClientDistributionActivity.class);
			break;
		case R.id.imageview_indent:
			skip(IndentManageActivity.class);
			break;
		case R.id.imageview_settings:
			skip(SettingsManageActivity.class);
			break;
		case R.id.imageview_detection_item:
			Intent detectionItemIntent = new Intent(this, DetectionItemActivity.class);
			detectionItemIntent.putExtra("intentMark", "MainActivity");
			startActivity(detectionItemIntent);
			break;
		case R.id.imageview_food_additives:
			link();
			break;
		case R.id.imageview_exit:
			skip(LoginActivity.class);
			MyApplication.getApplicationInstance().currentUser = new User();
			finish();
			break;
		}
	}

	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}

	/** 打开链接 */
	private void link() {
		String url = MyApplication.getApplicationInstance().foodAdditivesUrl;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		startActivity(intent);
	}
}
